//
//  SWPoetryDetailViewController.swift
//  ChildStory
//
//  Created by linhan on 2016/12/9.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWPoetryDetailViewController: SWBaseViewController,UIWebViewDelegate
{
    weak var poetry:SWPoetry?{
        didSet{
            if let poetry = poetry{
                update(poetry)
            }
        }
    }
    
    private var _inited:Bool = false
    private var _webView:UIWebView?
    private var _indicatorView:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    deinit
    {
        trace("DEINIT SWPoetryDetailViewController")
    }
    
    private func setup()
    {
        print("SWPoetryDetailViewController", view.frame)
        
        _webView = UIWebView(frame: view.bounds)
        _webView?.delegate = self
        view.addSubview(_webView!)
        
        _indicatorView.center = view.center
        view.addSubview(_indicatorView)
        
        _inited = true
        if let data = poetry
        {
            poetry = data
        }
        
    }
    
    private func update(_ poetry:SWPoetry)
    {
        if let urlString = poetry.contentURL,
           let url = URL(string: urlString)
        {
            _webView?.loadRequest(URLRequest(url: url))
        }
    }
    
    func webViewDidStartLoad(_ webView: UIWebView)
    {
        _indicatorView.startAnimating()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        _indicatorView.stopAnimating()
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
    {
        _indicatorView.stopAnimating()
    }
    
    
}
